package com.yipinhouse.backstage.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.ImmutableMap;
import com.yipinhouse.backstage.client.BrokerInfoWebQueryClient;
import com.yipinhouse.backstage.client.param.LoginWebParam;
import com.yipinhouse.backstage.common.utils.JwtUtil;
import com.yipinhouse.backstage.common.utils.WebUtil;
import com.yipinhouse.common.result.Result;
import com.yipinhouse.employee.client.BrokerInfoQueryClient;
import com.yipinhouse.employee.client.param.LoginParam;
import com.yipinhouse.employee.client.result.BrokerInfoResult;
import com.yipinhouse.employee.client.result.PromotionResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("backstage")
public class BrokerInfoWebQueryApiImpl implements BrokerInfoWebQueryClient {

    @Resource
    private BrokerInfoQueryClient brokerInfoQueryClient;

    /**
     * 员工登录
     *
     * @return
     */
    @Override
    public Result login(String account, String password) {
        log.info("员工登录：{}", account);
        LoginParam loginParam = new LoginParam();
        loginParam.setAccount(account);
        loginParam.setPassword(password);
        Result result = brokerInfoQueryClient.login(loginParam);
        Integer data = (Integer) result.getData();
        if (data != null) {
            HashMap<String, Object> map = new HashMap<>(ImmutableMap.of("brokerId", data, "account", account));
            String token = JwtUtil.createToken(map, 1440);
            return Result.getInstance(String.class).setMsg("登录成功").setCode(200).setData(token);
        }
        return Result.getInstance().setMsg("登录失败").setCode(500);
    }

    @Override
    public Result<BrokerInfoResult> getBrokerInfo() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId1 = request.getHeader("brokerId");
        Integer brokerId = Integer.valueOf(userId1);
        Result<BrokerInfoResult> brokerInfo = brokerInfoQueryClient.getBrokerInfo(brokerId);
        return brokerInfo;
    }

}
